package com.uow.algdemo.ui;

import java.util.ArrayList;
import java.util.HashMap;

import com.uow.algdemo.ui.R;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends Activity
{
    /**
     * List of algorithms' name.
     */
    private String[] algorithmsList = new String[16];
    /**
     * Filted algorithms' list by keyword.
     */
    private String[] algorithmsFilted;
    /**
     * Image resources for different algorithms.
     */
    private int[] imageResource = new int[16];
    /**
     * List view of algorithms.
     */
    private ListView list;
    /**
     * Main method of activity.
     */
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        algorithmsList[0] = "Quick Sort";
        algorithmsList[1] = "Bubble Sort";
        algorithmsList[2] = "Selection Sort";
        algorithmsList[3] = "Insertion Sort";
        algorithmsList[4] = "Shell Sort";
        algorithmsList[5] = "Binary Tree";
        algorithmsList[6] = "Binary Search Tree";
        algorithmsList[7] = "The Towers of Hanoi";
        algorithmsList[8] = "Queens problem";
        algorithmsList[9] = "Dijkstra's Algorithm";
        algorithmsList[10] = "Kruskal's Algorithm";
        algorithmsList[11] = "Prim's Algorithm";
        algorithmsList[12] = "Knuth Morris Pratt";
        algorithmsList[13] = "Morris Pratt";
        algorithmsList[14] = "Karp Rabin";
        algorithmsList[15] = "Boyer Moore";
        
        imageResource[0] = R.drawable.ic_logo_sort;
        imageResource[1] = R.drawable.ic_logo_sort;
        imageResource[2] = R.drawable.ic_logo_sort;
        imageResource[3] = R.drawable.ic_logo_sort;
        imageResource[4] = R.drawable.ic_logo_sort;
        imageResource[5] = R.drawable.ic_logo_tree;
        imageResource[6] = R.drawable.ic_logo_tree;
        imageResource[7] = R.drawable.ic_logo_hanoi;
        imageResource[8] = R.drawable.ic_logo_queen;
        imageResource[9] = R.drawable.ic_logo_graph;
        imageResource[10] = R.drawable.ic_logo_graph;
        imageResource[11] = R.drawable.ic_logo_graph;
        imageResource[12] = R.drawable.ic_logo_string;
        imageResource[13] = R.drawable.ic_logo_string;
        imageResource[14] = R.drawable.ic_logo_string;
        imageResource[15] = R.drawable.ic_logo_string;

        final EditText editKeyword = (EditText) findViewById(R.id.editSearch);
        editKeyword.setInputType(InputType.TYPE_CLASS_TEXT);
        editKeyword.addTextChangedListener(new TextWatcher()
        {
			public void afterTextChanged(Editable s)
            {
            	int count = 0;
            	String key = editKeyword.getText().toString().toLowerCase();
            	for(int i = 0; i < algorithmsList.length; i++)
            	{
            		if(algorithmsList[i].toLowerCase().contains(key))
            		{
            			count++;
            		}
            	}

            	algorithmsFilted = new String[count];

            	ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
                for(int i = 0, j = 0; i < algorithmsList.length; i++)
                {
            		if(algorithmsList[i].toLowerCase().contains(key))
            		{
            			algorithmsFilted[j] = algorithmsList[i];
	                    HashMap<String, Object> map = new HashMap<String, Object>();
	                    map.put("ItemTitle", algorithmsList[i]);
	                    map.put("ItemImage", imageResource[i]);
	                    listItems.add(map);
	                    j++;
            		}
                }

                SimpleAdapter listItemAdapter = new SimpleAdapter(MainActivity.this,listItems,
                        R.layout.layout_list,
                        new String[] {"ItemTitle", "ItemImage"}, 
                        new int[] {R.id.ItemTitle, R.id.ItemImage}
                );
                list.setAdapter(listItemAdapter);
            }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        }); 

        list = (ListView) findViewById(R.id.listAlgorithms);
        algorithmsFilted = algorithmsList;

        ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
        for(int i = 0; i < algorithmsFilted.length; i++)
        {
            HashMap<String, Object> map = new HashMap<String, Object>();
            map.put("ItemTitle", algorithmsFilted[i]);
            map.put("ItemImage", imageResource[i]);
            listItems.add(map);
        }
        SimpleAdapter listItemAdapter = new SimpleAdapter(this,listItems,
                R.layout.layout_list,
                new String[] {"ItemTitle", "ItemImage"}, 
                new int[] {R.id.ItemTitle, R.id.ItemImage}
        );
        list.setAdapter(listItemAdapter);
        list.setOnItemClickListener(new OnItemClickListener()
        {
            public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3)
        	{
            	final Intent i=new Intent();
            	i.setClass(MainActivity.this,AnimationActivity.class);
            	i.putExtra("title", algorithmsFilted[arg2]);
            	int index = -1;
            	for(int j = 0; j < algorithmsList.length; j++)
            	{
            		if(algorithmsList[j].equals(algorithmsFilted[arg2]))
            		{
            			index = j;
            			break;
            		}
            	}
            	i.putExtra("algorithmId", Integer.toString(index));
                MainActivity.this.startActivity(i);
        	}
        });
    }
}
